﻿using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Model;
using Model.Model;
using Model.View;

namespace Utils.UserHelper;

public class UserLogin
{
    public static LoginModel? LoginUser { get; set; }

    public static async Task<bool> Login(ViewUser user)
    {
        await using var db = new DataDbContent();
        var userInfo = await db.LoginUser!.FirstOrDefaultAsync(e =>
            e.Account == user.Account && e.Password == user.Password);
        if (userInfo is null)
            return false;
        UserLogin.LoginUser = userInfo;

        if (user.Memory)
        {
            Task.Factory.StartNew(async () => await UserMemory.MemoryUser(user));
        }

        return true;
    }
}